/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddUser.java
 *
 * Created on Nov 5, 2011, 4:23:13 PM
 */
package mpinventory;

import java.sql.*;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
/**
 *
 * @author msi
 */
public class AddUser extends javax.swing.JPanel {

    private Connection conn;
    private Dimension size;
    private UserQuery userQuery;
    public int width;
    public int height;
    public int screenWidth;
    public int screenHeight;
    public boolean backToMainMenu=false;
    private String position;
    /** Creates new form AddUser */
    public AddUser() {
        initComponents();
    }
    public AddUser(Connection conn, int screenWidth, int screenHeight) {
        this.conn=conn;
        this.screenWidth=screenWidth;
        this.screenHeight=screenHeight;
        initComponents();
        size=this.getPreferredSize();
        width=size.width;
        height=size.height+35;
        userQuery=new UserQuery(conn);
        position=cbxSUPosition.getSelectedItem().toString();
        cbxSUPosition.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent actionEvent){
                try{
                    position=cbxSUPosition.getSelectedItem().toString();
                }catch(Exception e){}
            }
        });
    }
    @Override
    public int getWidth(){
        return width;
    }
    @Override
    public int getHeight(){
        return height;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel5 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        btnSUASave = new javax.swing.JButton();
        btnSUACancel = new javax.swing.JButton();
        txtSULastname = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtSUFirstname = new javax.swing.JTextField();
        txtSUUsername = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jPasswordField2 = new javax.swing.JPasswordField();
        jPasswordField1 = new javax.swing.JPasswordField();
        cbxSUPosition = new javax.swing.JComboBox();

        jLabel5.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel5.setText("Username:");

        jLabel1.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 25)); // NOI18N
        jLabel1.setText("Add User Information");

        jLabel6.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel6.setText("Password:");

        btnSUASave.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        btnSUASave.setText("Save");
        btnSUASave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveActionPerformed(evt);
            }
        });

        btnSUACancel.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        btnSUACancel.setText("Cancel");
        btnSUACancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });

        txtSULastname.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));

        jLabel3.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel3.setText("Last Name:");

        jLabel2.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel2.setText("First Name:");

        txtSUFirstname.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));

        txtSUUsername.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));

        jLabel4.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel4.setText("Position:");

        jLabel8.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel8.setText("Confirm Password:");

        jPasswordField2.setFont(new java.awt.Font("Arial", 0, 15));

        jPasswordField1.setFont(new java.awt.Font("Arial", 0, 15));

        cbxSUPosition.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15)); // NOI18N
        cbxSUPosition.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Managing Director", "Operations Manager", "Warehouse Staff", "Mechanical Engineer", "Accountant" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(103, 103, 103)
                        .addComponent(btnSUASave, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(57, 57, 57)
                        .addComponent(btnSUACancel, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel2)
                            .addComponent(jLabel8)
                            .addComponent(jLabel6)
                            .addComponent(jLabel5)
                            .addComponent(jLabel3))
                        .addGap(32, 32, 32)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(cbxSUPosition, 0, 0, Short.MAX_VALUE)
                            .addComponent(txtSULastname)
                            .addComponent(txtSUFirstname, javax.swing.GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE)
                            .addComponent(txtSUUsername)
                            .addComponent(jPasswordField1)
                            .addComponent(jPasswordField2, javax.swing.GroupLayout.DEFAULT_SIZE, 231, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(91, 91, 91)
                        .addComponent(jLabel1)))
                .addContainerGap(23, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSUFirstname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSULastname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbxSUPosition, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSUUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jPasswordField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSUASave)
                    .addComponent(btnSUACancel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
        if(txtSUFirstname.getText().equals("") || txtSULastname.getText().equals("") || txtSUUsername.getText().equals("") || jPasswordField1.getText().equals("") || jPasswordField2.getText().equals(""))
            backToMainMenu=true;
        else{
            JOptionPane pane=new JOptionPane("Are you sure you want to go back to the main menu? Please make sure that all changes have been saved.");
            Object[] options=new String[]{"Yes","No"};
            pane.setOptions(options);
            JDialog dialog = pane.createDialog(new JFrame(), "Back To Main Menu?");
            dialog.show();
            if(options[0].equals(pane.getValue())) //YES
                backToMainMenu=true;
        }
    }//GEN-LAST:event_cancelActionPerformed

    private void saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveActionPerformed
        userQuery=new UserQuery(conn);
        if(txtSUFirstname.getText().equals("") || txtSULastname.getText().equals("") || txtSUUsername.getText().equals("") || jPasswordField1.getText().equals("") || jPasswordField2.getText().equals(""))
            JOptionPane.showMessageDialog(null, "All fields cannot be empty!");
        else if(jPasswordField1.getText().length()<6)
            JOptionPane.showMessageDialog(null, "Password too short!");
        else if(!jPasswordField1.getText().equals(jPasswordField2.getText()))
            JOptionPane.showMessageDialog(null, "The passwords do not match!");
        else if(userQuery.canAddUser(txtSUFirstname.getText(), txtSULastname.getText())){
            JOptionPane pane=new JOptionPane("Are you sure you want to add this user?");
            Object[] options=new String[]{"Yes","No"};
            pane.setOptions(options);
            JDialog dialog = pane.createDialog(new JFrame(), "Confirm User Addition");
            dialog.show();
            if(options[0].equals(pane.getValue())){
                userQuery.addUser(txtSUFirstname.getText(),txtSULastname.getText(),position,txtSUUsername.getText(),jPasswordField1.getText());
                backToMainMenu=true;
            } //YES
        }
        else
            JOptionPane.showMessageDialog(null, txtSUFirstname.getText() + " " + txtSULastname.getText() + " already exists in the database!");
    }//GEN-LAST:event_saveActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSUACancel;
    private javax.swing.JButton btnSUASave;
    private javax.swing.JComboBox cbxSUPosition;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JPasswordField jPasswordField2;
    private javax.swing.JTextField txtSUFirstname;
    private javax.swing.JTextField txtSULastname;
    private javax.swing.JTextField txtSUUsername;
    // End of variables declaration//GEN-END:variables
}
